package Offer038;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

/**
 * 每日温度
 * @author 23737
 * @time 2021.12.19
 */
public class Test {
    public static void main(String[] args) {

    }
}

/**
 * 单调栈
 */
class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        Deque<Integer> stack = new LinkedList<>();
        int n = temperatures.length;
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            while (!stack.isEmpty() && temperatures[stack.peekLast()] < temperatures[i]) {
                res[stack.peekLast()] = i - stack.pollLast();
            }
            stack.offerLast(i);
        }
        return res;
    }
}

/**
 * 暴力
 */
class Solution2 {
    public int[] dailyTemperatures(int[] temperatures) {
        int[] res = new int[temperatures.length];
        for (int i = 0; i < temperatures.length; i++) {
            for (int j = i + 1; j < temperatures.length; j++) {
                if (temperatures[j] > temperatures[i]) {
                    res[i] = j - i;
                    break;
                }
            }
        }
        return res;
    }
}